$(function(){
	loadItemList();
	selectItemListBuyer(false);
	selectBuyerGrades();
	//selectItemCatTree();
});

/**
 * 加载会员列表
 */
function loadItemList(){
	$('#buyerList_dateGrid').datagrid({
		loadMsg : "数据加载中",
		title:"会员列表",
		border:true,
		fit : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : false,
		toolbar : "#buyerList_dateGrid_toolbar",
		columns : [ [
		    {field:	 'checked',checkbox:true},
		    {field : 'id',hidden : true},
		    {field : 'email',hidden : true},
		    {field : 'gender',hidden : true},
		    {field : 'password',hidden : true},
		    {field : 'realname',hidden : true},
		    {field : 'regtime',hidden : true},
		    {field : 'visible',hidden : true},
		    {field : 'address',hidden : true},
		    {field : 'phone',hidden : true},
		    {field : 'postalcode',hidden : true},
		    {field : 'province',hidden : true},
		    {field : 'city',hidden : true},
		    {field : 'county',hidden : true},
		    {field : 'street',hidden : true},
		    {field : 'birthday',hidden : true},
		    {field : 'nickname',hidden : true},
		    {field : 'source',hidden : true},
			{field : 'username',	title : '用户名',	width : 80,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'mobile',	title : '手机号',		width : 100,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'levelName',title : '会员等级',	width : 40,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return value;
				},
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
					var str = '<a href="#" style="color:blue;margin-left:3px;" onClick="deleteBuyer('+row.id+')">删除</a>';
					str += ' |<a href="#" style="color:blue;margin-left:3px;" onClick="showBuyer('+row.id+')">查看</a>';
				    return str;
				}
			}
		] ],
		toolbar : [ {
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				deleteBuyer();
			}		
		}, ]
	});
}

/**
 * 查询会员列表
 * @param haveparams 是否有参数，初试加载传入false，点击查询按钮传入true
 */
function selectItemListBuyer(haveparams){
	var params = {};
	
	if(haveparams){
		var userName=$.trim($("#list_userName").val());          		//会员名
		var realName=$.trim($('#list_realName').val());                  //会员真是姓名
		var userLevel=$.trim($('#list_userLevel').combobox('getValue'));                 		//会员等级
		if(saleStatus == ''){
			saleStatus = [1,2,3];
		}else{
			saleStatus=[parseInt(saleStatus)];
		}
		params = {
			saleStatus : saleStatus,
			userName : userName,
			realName : realName,
			userLevel : userLevel,
		};
	}else{
		var saleStatus = [1,2,3];
		params = {
				saleStatus : saleStatus
			};
	}
	$("#buyerList_dateGrid").datagrid_pagination($("#buyerList_dateGrid"),path+"/buyer/v1/search",params,"","");
}

/**
 * 查询所有会员等级（用于下拉菜单）
 */
function selectBuyerGrades(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/buyer/v1/selectBuyerGrade?page=1&rows=10000',
		data:'{}',
		dataType:"json",
		success:function(data){
			$("#list_userLevel").combobox({
				valueField : 'id',
				textField : 'name',
				data : data.rows
			});
		}
   });
}

/**
 * 查看会员
 */
function showBuyer(id){
	if(id != null && id != ''){
		var params = {id:id};
    	$.ajax({
    		type:"post",
    		//contentType:"application/json; charset=utf-8",
    		url:path+'/buyer/v1/showBuyer',
    		data:params,
    		dataType:"json",
    		success:function(data){
    			$('#id').val(data.id);
				$('#username').val(data.username);
				$('#levelName').val(data.levelName);
				$('#nickname').val(data.nickname);
    			$('#realname').val(data.realname);
    			$('#birthday').val(data.birthday);
    			$('#gender').val(data.gender);
    			$('#email').val(data.email);
    			$('#mobile').val(data.mobile);
    			$('#phone').val(data.phone);
    			$('#address').val(data.province + " " + data.city + " " + data.county);
    			$('#streetAddress').val(data.street);
    			$('#regtime').val(new Date(data.regtime).format("yyyy-MM-dd hh:mm:ss"));
    		}
       });
	}
	$('#itemBuyerWindow').window("open");
}


/**
 * 删除会员
 */
function deleteBuyer(id){
	if(id == '' || id == undefined){
		id = getSelectionsIds();
		if(id.length == 0){
			$.messager.alert('提示','未选中开票资料!');
			return ;
		}
	}
	$.messager.confirm('确认','确定删除此会员吗？',function(r){
	    if (r){
	    	var params = {};
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/buyer/v1/deleteuser/' + id,
	    		data:params,
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除会员成功!',undefined,function(){
	    					selectItemListBuyer(false);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}


/**
 * 得到所选择行的id（int型）
 * @returns {Array}
 */
function getSelectionsIds(){
	var itemList = $("#buyerList_dateGrid");
	var sels = itemList.datagrid("getSelections");
	var ids = new Array();
	for(var i in sels){
		ids.push(parseInt(sels[i].id));
	}
	return ids;
}



/**
 * 编辑商品，跳转到发布页面
 */
function editItem(){

	var ids = getSelectionsIds();
	if(ids.length == 0){
		$.messager.alert('提示','必须选择一个商品才能编辑!');
		return ;
	}
	if(ids.length > 1){
		$.messager.alert('提示','只能选择一个商品!');
		return ;
	}
	
	row = $("#buyerList_dateGrid").datagrid("getSelected");
	
	$(".tree-title:contains('商品发布')").parent().click();
	
	var tab = $('#tabs').tabs('getSelected');  // 获取选择的面板
	tab.panel('refresh', 'auemall-item-add');

}

